import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv } from "vite";

import { createPlugins } from "./configs/plugins";
import { createServer } from "./configs/server";

// https://vitejs.dev/config/
export default ({ mode }: { mode: string }) => {
  const envs = loadEnv(mode, process.cwd());

  return defineConfig({
    base: "./",
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    server: createServer(mode, envs),
    plugins: createPlugins(mode, envs),
    css: {
      // lightningcss: {
      //   cssModules: {
      //     pattern: "[name]_[local]_[hash:base64:8]",
      //     dashedIdents: true,
      //   },
      // },
      modules: {
        // generateScopedName: "[name]_[local]_[hash:base64:8]",
        generateScopedName: "[local]__[hash:base64:8]",
        localsConvention: "camelCaseOnly",
      },
    },
    // 编译选项
    build: {
      // rollupOptions: {
      //   // rollup 进行打包时标识的外部依赖
      //   external: [],
      // },

      terserOptions: {
        compress: {
          // 编译时去除 console 以及 debugger
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
  });
};
